Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeat And Missing Number Array #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

927tanmay
Copy link

Added new solution

Copy link
Owner

@cruxrebels cruxrebels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow the Guidelines while making a pull request.

"Even though it might not be required in this problem, in some cases, you might be required to order the operations cleverly so that the numbers do not overflow.
For example, if you need to calculate n! / k! where n! is factorial(n), one approach is to calculate factorial(n), factorial(k) and then divide them.
Another approach is to only multiple numbers from k + 1 ... n to calculate the result.
Obviously approach 1 is more susceptible to overflows."
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap all these comments to new lines so that it does not trails the editor view.

@@ -0,0 +1,52 @@
/*There are certain problems which are asked in the interview to also check how you take care of overflows in your problem.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Begin problem statement on a newline after the block comment.

/*
There are certain problems...

For example, if you need to calculate n! / k! where n! is factorial(n), one approach is to calculate factorial(n), factorial(k) and then divide them.
Another approach is to only multiple numbers from k + 1 ... n to calculate the result.
Obviously approach 1 is more susceptible to overflows."

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File name must be RepeatAndMissingNumberArray.cpp.

Comment on lines +36 to +37
int n =A.size();
long long int sq1=0,sum1=0,sum2=0,sq2=0,x,a;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give 1 space before and after each assignment operator.

a = 10;

vector<int> Solution::repeatedNumber(const vector<int> &A) {
int n =A.size();
long long int sq1=0,sum1=0,sum2=0,sq2=0,x,a;
vector <int> v(2,0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vector<int> v(2, 0);

long long int sq1=0,sum1=0,sum2=0,sq2=0,x,a;
vector <int> v(2,0);
int i;
for(i=0;i<n;i++){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (i=0; i<n; ++i) 
{

Comment on lines +41 to +50
sum2=sum2+long(i+1);
sq2=sq2+long(i+1)*long(i+1);
sum1=sum1+long(A[i]);
sq1=sq1+long(A[i])*long(A[i]);
}

x=(sq1-sq2)/(sum1-sum2);
a=(x+(sum1-sum2))/2;
v[0]=a;
v[1]=x-a;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Give appropriate spacing, refer existing code files.
  2. Add code logical comments as what your each block is doing.

@cruxrebels
Copy link
Owner

Hi @927tanmay, thanks for your PR. Kindly address the comments and follow https://github.com/cruxrebels/InterviewBit#how-to-contribute to get this merged! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants